/**   
 * @Title: SportteryMainThreadFather.java 
 * @Package com.esun.rabbit2.viewer.mainact.lotteryhallact 
 * @Description: 竞技彩刷新主线程抽象类
 * @author: Billy Qigs@500wan.com   
 * @date: 2012-4-12 下午6:16:28  
 * Copyright: (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.rabbit2.viewer.mainact.lotteryhallact;

import com.esun.config.Config;
import com.esun.config.Key;
import com.esun.config.Language;
import com.esun.rabbit2.communicaters.beijingdanchang.BeiDanCurrentExpectCommunicater;
import com.esun.ui.R;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * @ClassName: SportteryMainThreadFather
 * @Description: 竞技彩刷新主线程抽象类
 * @author: Billy Qigs@500wan.com
 * @date: 2012-4-12 下午6:16:28
 * 
 */
public class BeiDanMainThread {

	private LinearLayout linearItem;
	private ProgressBar pb;
	private TextView tvMsg;
	private TextView tvExpect;
	private LinearLayout linearExpect;
	private Handler handler;

	private BeiDanCurrentExpectCommunicater currentExpectCommunicater;

	private final int GET_CURRENT_EXPECT_SUCCEED = 10001;
	private final int GET_CURRENT_EXPECT_FAILED = 10002;

	public BeiDanMainThread(LinearLayout linearItem) {

		this.linearItem = linearItem;
		pb = (ProgressBar) this.linearItem.findViewById(R.id.lotteryhall_act_pb_beidan);
		tvMsg = (TextView) this.linearItem.findViewById(R.id.lotteryhall_act_tv_beidan_msg);
		tvExpect = (TextView) this.linearItem.findViewById(R.id.lotteryhallact_tv_tobeidan_expect);
		linearExpect = (LinearLayout) this.linearItem.findViewById(R.id.lotteryhallact_ll_tobeidan_expect);

		initHandler();

	}

	// 刷新
	public void refresh() {
		progressShow();
		getDataThread();
	}

	// 获取北单期号信息
	public void getDataThread() {

		new Thread() {

			@Override
			public void run() {
				if (currentExpectCommunicater == null) {
					currentExpectCommunicater = new BeiDanCurrentExpectCommunicater();
				}

				Message msg;

				try {
					boolean isSucceed = currentExpectCommunicater.isGetCurrentExpectSucceed();

					if (isSucceed) {

						String expect = currentExpectCommunicater.getResult().get(Key.KEY_EXPECT);

						msg = handler.obtainMessage(GET_CURRENT_EXPECT_SUCCEED, expect);
					} else {
						msg = handler.obtainMessage(GET_CURRENT_EXPECT_FAILED, null);
					}
				} catch (Exception e) {
					msg = handler.obtainMessage(Config.CONNECTION_FAIL, null);
				}
				handler.sendMessage(msg);
			}
		}.start();

	}

	// 显示获取成功view
	public void showSucceedView(String expect) {
		linearExpect.setVisibility(View.VISIBLE);
		tvExpect.setText(expect);
	}

	// 显示获取失败view
	public void showFailedView() {
		linearExpect.setVisibility(View.GONE);
		tvMsg.setText(Language.MSG_BEIDAN_LOTTERYHALL_TIP);
	}

	// 显示彩种停售view
	public void showStopView() {
		linearExpect.setVisibility(View.GONE);
		tvMsg.setText(Language.MSG_STOP_LOTTERY);
	}

	// 显示进度圈
	public void progressShow() {
		pb.setVisibility(View.VISIBLE);
	}

	// 关闭进度圈
	public void progressDismiss() {
		pb.setVisibility(View.GONE);
	}

	private void initHandler() {
		handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {

				progressDismiss();

				switch (msg.what) {

				// 获取期号信息成功
				case GET_CURRENT_EXPECT_SUCCEED:

					String expect = (String) msg.obj;

					showSucceedView("第" + expect + "期");

					break;
				// 获取期号信息失败
				case GET_CURRENT_EXPECT_FAILED:

					showFailedView();

					break;

				// 获取期号信息异常
				case Config.CONNECTION_FAIL:

					showFailedView();

					break;

				default:

					showFailedView();

					break;

				}

			}

		};
	}

}
